class StockLoggingsController < ApplicationController
  before_action :set_stock_logging, only: [:show, :edit, :update, :destroy]

  # GET /stock_loggings
  # GET /stock_loggings.json
  def index
    @stock_loggings = StockLogging.all
  end

  # GET /stock_loggings/1
  # GET /stock_loggings/1.json
  def show
  end

  # GET /stock_loggings/new
  def new
    @stock_logging = StockLogging.new
  end

  # GET /stock_loggings/1/edit
  def edit
  end

  # POST /stock_loggings
  # POST /stock_loggings.json
  def create
    @stock_logging = StockLogging.new(stock_logging_params)
    @drug = Drug.find(@stock_logging.drug_id)
    
    #find existing stock
    @stocks = Stock.find_by(drug_id: @stock_logging.drug_id, inventory_id: @stock_logging.inventory_id)
    
    if (@stocks != nil)
    	if (@stock_logging.stock_logging_category_id != 1)
    		@stock_logging.update(:quantity => (@stock_logging.quantity*-1)) 
    	end
    	update_stock = @stocks.quantity + @stock_logging.quantity
    	@stocks.update(:quantity => update_stock, :inventory_id => @stock_logging.inventory_id)
    	@stock_logging.update(:stock_id => @stocks.id, :inventory_id => @stock_logging.inventory_id)
    else 	
		@stock = @drug.stocks.create(:drug_id => @drug.id, :inventory_id => @stock_logging.inventory_id, :quantity => @stock_logging.quantity)
		@stock_logging.update(:stock_id => @stock.id, :inventory_id => @stock_logging.inventory_id)		
    end
    #create the drug stock_logging links
    @drug.stock_loggings << @stock_logging

    respond_to do |format|
      if @stock_logging.save
        format.html { redirect_to @drug, notice: 'Stock logging was successfully created.' }
        format.json { render :show, status: :created, location: @stock_logging }
      else
        format.html { render :new }
        format.json { render json: @stock_logging.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /stock_loggings/1
  # PATCH/PUT /stock_loggings/1.json
  def update
    respond_to do |format|
      if @stock_logging.update(stock_logging_params)
        format.html { redirect_to @stock_logging, notice: 'Stock logging was successfully updated.' }
        format.json { render :show, status: :ok, location: @stock_logging }
      else
        format.html { render :edit }
        format.json { render json: @stock_logging.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /stock_loggings/1
  # DELETE /stock_loggings/1.json
  def destroy
    @stock_logging.destroy
    respond_to do |format|
      format.html { redirect_to stock_loggings_url, notice: 'Stock logging was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_stock_logging
      @stock_logging = StockLogging.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def stock_logging_params
      params.require(:stock_logging).permit(:drug_id, :stock_id, :quantity, :inventory_id, :stock_logging_category_id, :created_at, :updated_at, :created_by, :updated_by)
    end
    
    def stock_params
      params.require(:stock).permit(:drug_id, :inventory_id, :quantity, :created_at, :updated_at, :created_by, :updated_by)
    end
end
